﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using ERPStudio;
using System.Data.SqlClient;

namespace ExBug
{
    public partial class OrdonantariRezFrm : FrmRezumat
    {
        private BindingSource bs = null;
        public Int64 IDPartener = -1;

        public OrdonantariRezFrm(BindingSource _bs, ModuleClass mInfo)
            : base(mInfo)
        {
            if (_bs != null)
            {
                InitializeBindingSource(_bs);
                bs = _bs;
            }

            InitializeComponent();
            workConnString = mInfo.workConnectionString;
        }

        private void OrdonantariRezFrm_Load(object sender, EventArgs e)
        {
            listTableAdaptor.Add(ordonantariTableAdapter.Adapter);

            SqlConnection conn = DeschideConexiune(workConnString);
            this.parteneriTableAdapter.Connection = conn;
            this.ordonantariTableAdapter.Connection = conn;
            jurnaleTableAdapter.Connection = conn;
            //this.localitatiTableAdapter.Connection = conn;
            //this.banciTableAdapter.Connection = conn;
            //this.banciTableAdapter.Fill(this.eSRPDataSet.Banci);
            //this.localitatiTableAdapter.Fill(this.eSRPDataSet.Localitati);
            //this.judeteTableAdapter.Connection = conn;
            //this.judeteTableAdapter.Fill(this.eSRPDataSet.Judete);
            jurnaleTableAdapter.Fill(eSRPDataSet.Jurnale, ModuleInfo.CodUnitate);
            this.parteneriTableAdapter.Fill(this.eSRPDataSet.Parteneri);
            if (listBndSourceMain.Count == 0)
                this.ordonantariTableAdapter.Fill(eSRPDataSet.Ordonantari,ModuleInfo.CodUnitate);
            InchideConexiune(conn);
            AddButton("Nota Contabila", global::ERPStudio.Properties.Resources._001_53, GenerareNotaContabila, ToolStripItemAlignment.Right, true);
            /// functia care se apeleaza in momentul in care se apasa F9
            HandleSalvare += Salvare; 
            if (listBndSourceMain.Count > 0)
                ordonantariDGV.DataSource = listBndSourceMain[0];
            ordonantariBindingSource.Filter = String.Empty;
            if (IDPartener != -1)
                ordonantariBindingSource.Filter = "IDFurnizor=" + IDPartener.ToString();
 
        }

        private void CautaLocal(object sender, EventArgs e)
        {

            EnableMainSearchButton = false;
            OrdonantariSearchFrm frm = new OrdonantariSearchFrm(ModuleInfo,true/* ParteneriProprii*/, true /*AfiseazaDoarDocumenteleCurente*/);
            if (frm.ShowDialog() == DialogResult.OK)
            {
                if (frm.Filtru == String.Empty)
                    return;
                listBndSourceMain[0].Filter = frm.Filtru;
                /*ordonantariTableAdapter.Connection = DeschideConexiune(workConnString);
                ordonantariTableAdapter.FillByFiltru(eSRPDataSet.Ordonantari, frm.Filtru, ModuleInfo.AnArhiva);
                InchideConexiune(ordonantariTableAdapter.Connection);*/
            }
            else
                listBndSourceMain[0].Filter = String.Empty;
            EnableMainBrowseButton = true;
            EnableMainSearchButton = true;
        }


        private void GenerareNotaContabila(object sender, EventArgs e)
        {

            if (ordonantariDGV.SelectedRows.Count == 0)
            {
                MessageBoxFrm mbt = new MessageBoxFrm("Eroare", "Va rugam selectati ordonantarile pentru care se doreste generarea notei contabile!", String.Empty, MessageBoxFrm.ImaginiMB.StopIcon, null);
                mbt.AddButton("Iesire", 70, mbt.MessageBoxFrm_Click_Yes);
                mbt.ShowDialog();
                return;
            }



            if (bs==null || bs.Current == null)
                return;
            while (((DataRowView)bs.Current).IsNew)
            {
                Salvare(sender, e);
                if (EroareSalvare)
                    return;
            }
            String OrdonantariSelectate = string.Empty;
            String OrdonantariSelectateID = string.Empty;
            foreach (DataGridViewRow dgvr in ordonantariDGV.SelectedRows)
            {
                ESRPDataSet.OrdonantariRow or = ((DataRowView)dgvr.DataBoundItem).Row as ESRPDataSet.OrdonantariRow;
                if (or != null)
                {
                    if (OrdonantariSelectate != String.Empty)
                        OrdonantariSelectate += ",";
                    OrdonantariSelectate += or.NrDoc.ToString();

                    if (OrdonantariSelectateID != String.Empty)
                        OrdonantariSelectateID += ",";
                    OrdonantariSelectateID += or.IdOrdonantare.ToString();
                }
            }


            MessageBoxFrm mbt2 = new MessageBoxFrm("Confirmare", "Se genereaza o nota contabila pentru ordonantarile: " + OrdonantariSelectate, String.Empty, MessageBoxFrm.ImaginiMB.StopIcon, null);
            mbt2.AddButton("Genereaza", 70, mbt2.MessageBoxFrm_Click_Yes);
            mbt2.ShowDialog();

            SqlCommand cmd = new SqlCommand("exbug_OrdonantareGenNotaContabilaGroup", DeschideConexiune(workConnString));
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add(new SqlParameter("idOrdonantareGrup", OrdonantariSelectateID));
            cmd.Parameters.Add(new SqlParameter("CodUnitate", ModuleInfo.CodUnitate));
            try
            {
                cmd.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                InchideConexiune(cmd.Connection);
                MessageBoxFrm mbt = new MessageBoxFrm("Eroare", ex.Message, String.Empty, MessageBoxFrm.ImaginiMB.StopIcon, null);
                mbt.AddButton("Iesire", 70, mbt.MessageBoxFrm_Click_Yes);
                mbt.ShowDialog();
                return;
            }
            InchideConexiune(cmd.Connection);
            MessageBoxFrm mbt1 = new MessageBoxFrm("Confirmare", "Nota contabila a fost generata cu succes!", String.Empty, MessageBoxFrm.ImaginiMB.InformareIcon, null);
            mbt1.AddButton("Iesire", 70, mbt1.MessageBoxFrm_Click_Yes);
            mbt1.ShowDialog();
            


        }

    }
}
